package com.tanhua.server.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tanhua.model.mongo.Movement;
import com.tanhua.model.vo.MovementsVo;
import com.tanhua.model.vo.PageResult;
import com.tanhua.server.server.MovementsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.aggregation.ArrayOperators;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@RequestMapping("/movements")
public class MovementsController {

    @Autowired
    private MovementsService  movementsService;
    @PostMapping   //请求路径涉及文件，就一个参数一个参数的接收，，把post请求的请求体接收成map或者对象才需要用requestbody
    public ResponseEntity movements(MultipartFile imageContent[],String textContent,String location,String longitude,String latitude) throws IOException {
        movementsService.sendMovement(imageContent,textContent,location,longitude,latitude);
        return ResponseEntity.ok().body(null);
    }
    @GetMapping("/all")
    public ResponseEntity all(@RequestParam(defaultValue = "1") Integer page,
                              @RequestParam(defaultValue = "10")Integer pageSize,
                              Long userId){
       PageResult pageResult = movementsService.findAll(page,pageSize,userId);
       return ResponseEntity.ok(pageResult);
    }
    @GetMapping
    public ResponseEntity friendMovement(@RequestParam(defaultValue = "1")Integer page,
                                         @RequestParam(defaultValue = "10")Integer pageSize){
        PageResult pageResult = movementsService.friendsMovement(page,pageSize);
        return ResponseEntity.ok(pageResult); //拽过来，我操作不了
    }
    @GetMapping("/recommend")  //查找推荐动态
    public ResponseEntity recommend(@RequestParam(defaultValue = "1") Integer page,
                                    @RequestParam(defaultValue = "10")Integer pageSize){
    PageResult pageResult = movementsService.findRecommend(page,pageSize);
    return ResponseEntity.ok(pageResult);
    }
    @GetMapping("/{id}")
    public ResponseEntity movementById(@PathVariable("id") String id){
        MovementsVo vo = movementsService.findById(id);
        return ResponseEntity.ok(vo);
    }
    //todo 首页谁看过我功能  待完善  写出接口  防止报错
    @GetMapping("/visitors")
    public ResponseEntity seeMe(){
        return ResponseEntity.ok(null);
    }

    //点赞  文档中返回值没写名称，直接返回即可  如果有名字，就要构建map返回
    @GetMapping("/{id}/like")
    public ResponseEntity like(@PathVariable("id") String id){
        //调service
        Integer count = movementsService.like(id);
        return ResponseEntity.ok(count);
    }
    @GetMapping("/{id}/dislike")
    public ResponseEntity dislike(@PathVariable("id") String id){
        Integer count = movementsService.dislike(id);
        return  ResponseEntity.ok(count);
    }
    @GetMapping("/{id}/love")
    public ResponseEntity love(@PathVariable("id") String id){
        Integer count = movementsService.love(id);
        return ResponseEntity.ok(count);
    }
    @GetMapping("/{id}/unlove")
    public ResponseEntity unlove(@PathVariable("id") String id){
        Integer count = movementsService.unlove(id);
        return ResponseEntity.ok(count);
    }
}
